jQuery(document).ready(function(){

	var wizardpanelUrlParams = new URLSearchParams(window.location.search);
	var wizardpanelTab = wizardpanelUrlParams.get('tab');

	if (wizardpanelTab && jQuery('#' + wizardpanelTab).length) {

		jQuery('.wizardpanel div.admincontent').hide();
		jQuery('#' + wizardpanelTab).show();

		jQuery('.wizardpanel .wizardpanel-content-left li').removeClass('active');
		jQuery('.wizardpanel .wizardpanel-content-left li#z' + wizardpanelTab).addClass('active');

		jQuery('.wizardpanel-tabsmenu select').val(wizardpanelTab);

	} else {

		jQuery('.wizardpanel div.admincontent').hide();
		jQuery('.wizardpanel div.admincontent:first').show();
		jQuery('.wizardpanel .wizardpanel-content-left li:first').addClass('active');

	}
		jQuery('.wizardpanel .wizardpanel-content-left li a, .wizardpanel-tablink, .wizardpanel-active-design-button, .wizardpanel-header .wizardpanel-topmenu li a').click(function() {



	    jQuery('.wizardpanel .wizardpanel-content-left li').removeClass('active'); // Remove active class from links
		jQuery(this).parent().addClass('active'); //Set parent of clicked link class to active
		var currentTab = jQuery(this).attr('href'); // Set currentTab to value of href attribute
		jQuery('.wizardpanel div.admincontent').hide(); // Hide all divs
		jQuery('#wizardpanel-content-right').show();
		jQuery(currentTab).show(); // Show div with id equal to variable currentTab
		return false;
	});
	
	jQuery('.wizardpanel-tabsmenu select').change(function() {
	    var obj = jQuery(this);
		var currentTab = obj.val();
		jQuery('.wizardpanel div.admincontent').hide(); // Hide all divs
		jQuery("#" + currentTab).show(); 
		return false;
	});
	
});

// Content Tabs	

	(function($) {
    
    $.fn.tabdisplays = function ( options ) {
	var defaults = {
	    'defaultActive': 1
	},
	settings = $.extend({}, defaults, options);
	var obj = jQuery(this);
	var objid = obj.attr('id');
	
	var tabs = function () {
	   jQuery(obj).addClass("wizardpanel-tabdisplay"); 
	   jQuery( "#" + objid + " .wizardpanel-tablist li[data-tab='"+ settings.defaultActive +"']").addClass('wizardpanel-tabdisplayactive');
	   jQuery("#" + objid + " .wizardpanel-tabdisplay-" + settings.defaultActive).css({'display':'block'});
	   jQuery( "#" + objid + " .wizardpanel-tablist li").each(function (x,y) {
	       jQuery(y).click(function () {
	            jQuery("#" + objid + " .wizardpanel-tablist li").removeClass("wizardpanel-tabdisplayactive");
	            jQuery(y).addClass("wizardpanel-tabdisplayactive");
	            jQuery("#" + objid + " .wizardpanel-tabcontent").css( { "display":"none" } );
	            jQuery("#" + objid + " .wizardpanel-tabdisplay-" + jQuery(this).data('tab')).css({'display':'block'});
	       });
	    });
	}
	
	tabs();
	return this;
		
    }	
}(jQuery));


jQuery('document').ready(function () { // Set default tab 
    jQuery("#wizardpanel-tabblock1").tabdisplays( { 'defaultActive' : 3} );
    jQuery("#wizardpanel-tabblock2").tabdisplays( { 'defaultActive' : 1} );
    jQuery("#wizardpanel-tabblock3").tabdisplays( { 'defaultActive' : 1} );
    jQuery("#wizardpanel-tabblock4").tabdisplays( { 'defaultActive' : 1} );
    jQuery("#wizardpanel-tabblock5").tabdisplays( { 'defaultActive' : 1} );    
});

// Notice Hide

jQuery(document).ready(function(){
  setTimeout("jQuery('.noticehide').fadeOut('slow');",5000);
});
		
// Scroll to top

jQuery(document).ready(function() {

	var offset = 300,

		offset_opacity = 1200,
		scroll_top_duration = 700,
		$back_to_top = jQuery('.cd-top');

	jQuery(window).scroll(function(){
		( jQuery(this).scrollTop() > offset ) ? $back_to_top.addClass('cd-is-visible') : $back_to_top.removeClass('cd-is-visible cd-fade-out');
		if( jQuery(this).scrollTop() > offset_opacity ) { 
			$back_to_top.addClass('cd-fade-out');
		}
	});

	$back_to_top.on('click', function(event){
		event.preventDefault();
		jQuery('body,html').animate({
			scrollTop: 0 ,
		 	}, scroll_top_duration
		);
	});

});

// Image Upload

(function($) {

	$.fn.uploadMedia = function(options) {

		var defaults = {},
			settings = $.extend({}, defaults, options);

		var obj = jQuery(this);
		var mediaUploader;
		var wrapperid = "";

		jQuery(obj).click(function(e) {

			e.preventDefault();

			wrapperid = $(this).parent().next().find(".wizardpanel-form-textinput").attr("name");

			if (!$(this).parent().parent().is("div.wrapped_" + wrapperid)) {
				$(this).parent().addClass(wrapperid);
				$(this).parent().next().addClass(wrapperid);
				$(this).parent().next().next().addClass(wrapperid);
				$("." + wrapperid).wrapAll("<div id='" + wrapperid + "' class='wrapped_" + wrapperid + "'></div>");
			}

			if (mediaUploader) {
				mediaUploader.open();
				return;
			}

			mediaUploader = wp.media.frames.file_frame = wp.media({
				title: 'Choose a picture',
				button: {
					text: "Choose Picture"
				},
				multiple: false
			});

			mediaUploader.on('select', function() {

				var attachment = mediaUploader.state().get('selection').first().toJSON();

				$("#" + wrapperid + " input[name='" + wrapperid + "']").val(attachment.url);

				var previewHtml = '<img class="wizardpanel-field-imagepreview" src="' + attachment.url + '" />';

				if ($("#" + wrapperid + " .open-media-preview").length) {

					$("#" + wrapperid + " .open-media-preview").html(previewHtml);

				} else {

					$("#" + wrapperid + " input[name='" + wrapperid + "']")
						.closest(".wizardpanel-field")
						.after('<div class="wizardpanel-field open-media-preview">' + previewHtml + '</div>');

				}

				$("." + wrapperid).unwrap();
				$('.wizardpanel-field').removeClass(wrapperid);

			});

			mediaUploader.open();

		});

		var removeimagepreview = function() {
			$(".open-media-preview span").click(function() {
				$(this).parent().parent().find('[type=text]').val("");
				$(this).parent().html("");
			});
		};

		removeimagepreview();

		return this;

	};

}(jQuery));

jQuery(document).ready(function() {
	jQuery(".open-media").uploadMedia();
});